const Get = require('../template')

console.log("=== Get 性能测试 ===")

// 测试小对象获取性能
console.log(
  "小对象获取性能:", (() => {
    const obj = { a: 1, b: 2, c: 3 }
    const iterations = 100000

    const start = performance.now()
    for (let i = 0; i < iterations; i++) {
      Get.get(obj, 'a')
    }
    const end = performance.now()

    const avgTime = (end - start) / iterations
    return avgTime < 0.001 // 平均每次获取时间应该小于0.001ms
  })()
)

// 测试深度路径获取性能
console.log(
  "深度路径获取性能:", (() => {
    const obj = { a: { b: { c: { d: { e: 'deep' } } } } }
    const iterations = 100000

    const start = performance.now()
    for (let i = 0; i < iterations; i++) {
      Get.get(obj, 'a.b.c.d.e')
    }
    const end = performance.now()

    const avgTime = (end - start) / iterations
    return avgTime < 0.005 // 平均每次获取时间应该小于0.005ms
  })()
)

// 测试大对象获取性能
console.log(
  "大对象获取性能:", (() => {
    const obj = {}
    for (let i = 0; i < 1000; i++) {
      obj[`key${i}`] = { value: i, nested: { deep: i * 2 } }
    }

    const iterations = 10000

    const start = performance.now()
    for (let i = 0; i < iterations; i++) {
      Get.get(obj, `key${i % 1000}.nested.deep`)
    }
    const end = performance.now()

    const avgTime = (end - start) / iterations
    return avgTime < 0.01 // 平均每次获取时间应该小于0.01ms
  })()
)

// 测试数组路径获取性能
console.log(
  "数组路径获取性能:", (() => {
    const obj = { data: Array.from({ length: 1000 }, (_, i) => ({ id: i, value: i * 2 })) }
    const iterations = 10000

    const start = performance.now()
    for (let i = 0; i < iterations; i++) {
      Get.get(obj, `data.${i % 1000}.value`)
    }
    const end = performance.now()

    const avgTime = (end - start) / iterations
    return avgTime < 0.01 // 平均每次获取时间应该小于0.01ms
  })()
)

// 测试不存在的路径获取性能
console.log(
  "不存在的路径获取性能:", (() => {
    const obj = { a: 1 }
    const iterations = 100000

    const start = performance.now()
    for (let i = 0; i < iterations; i++) {
      Get.get(obj, 'nonexistent.path')
    }
    const end = performance.now()

    const avgTime = (end - start) / iterations
    return avgTime < 0.001 // 平均每次获取时间应该小于0.001ms
  })()
)

// 测试多路径获取性能
console.log(
  "多路径获取性能:", (() => {
    const obj = { a: { b: 1 }, c: { d: 2 }, e: { f: 3 } }
    const paths = ['a.b', 'c.d', 'e.f']
    const iterations = 10000

    const start = performance.now()
    for (let i = 0; i < iterations; i++) {
      Get.getMultiple(obj, paths, 'default')
    }
    const end = performance.now()

    const avgTime = (end - start) / iterations
    return avgTime < 0.01 // 平均每次获取时间应该小于0.01ms
  })()
)

// 测试条件获取性能
console.log(
  "条件获取性能:", (() => {
    const obj = { a: 1, b: 2, c: 3, d: 4 }
    const iterations = 100000

    const start = performance.now()
    for (let i = 0; i < iterations; i++) {
      Get.getWithCondition(obj, 'a', 'default', (value, key) => value > 0)
    }
    const end = performance.now()

    const avgTime = (end - start) / iterations
    return avgTime < 0.002 // 平均每次获取时间应该小于0.002ms
  })()
)

// 测试类型检查获取性能
console.log(
  "类型检查获取性能:", (() => {
    const obj = { a: 1, b: 'test', c: true }
    const iterations = 100000

    const start = performance.now()
    for (let i = 0; i < iterations; i++) {
      Get.getWithType(obj, 'a', 'default', 'number')
    }
    const end = performance.now()

    const avgTime = (end - start) / iterations
    return avgTime < 0.002 // 平均每次获取时间应该小于0.002ms
  })()
)

// 测试转换获取性能
console.log(
  "转换获取性能:", (() => {
    const obj = { a: 1, b: 'test' }
    const iterations = 100000

    const start = performance.now()
    for (let i = 0; i < iterations; i++) {
      Get.getWithTransform(obj, 'a', 'default', value => value * 2)
    }
    const end = performance.now()

    const avgTime = (end - start) / iterations
    return avgTime < 0.002 // 平均每次获取时间应该小于0.002ms
  })()
)

// 测试验证获取性能
console.log(
  "验证获取性能:", (() => {
    const obj = { name: 'John', age: 30 }
    const schema = { name: { type: 'string' }, age: { type: 'number' } }
    const iterations = 10000

    const start = performance.now()
    for (let i = 0; i < iterations; i++) {
      try {
        Get.getWithValidation(obj, 'name', 'default', schema)
      } catch (error) {
        // 忽略错误，测试性能
      }
    }
    const end = performance.now()

    const avgTime = (end - start) / iterations
    return avgTime < 0.01 // 平均每次获取时间应该小于0.01ms
  })()
)

// 测试统计获取性能
console.log(
  "统计获取性能:", (() => {
    const obj = { a: { b: { c: 1 } } }
    const iterations = 10000

    const start = performance.now()
    for (let i = 0; i < iterations; i++) {
      Get.getWithStats(obj, 'a.b.c', 'default')
    }
    const end = performance.now()

    const avgTime = (end - start) / iterations
    return avgTime < 0.01 // 平均每次获取时间应该小于0.01ms
  })()
)

// 测试异步获取性能
console.log(
  "异步获取性能:", (() => {
    const obj = { a: 1, b: 'test' }
    const iterations = 1000
    const promises = []

    const start = performance.now()
    for (let i = 0; i < iterations; i++) {
      promises.push(Get.getAsync(obj, 'a', 'default', async (value, key) => {
        await new Promise(resolve => setTimeout(resolve, 1))
        return value * 2
      }))
    }

    return Promise.all(promises).then(results => {
      const end = performance.now()
      const avgTime = (end - start) / iterations

      return results.length === iterations &&
        results.every(result => result === 2) &&
        avgTime < 5 // 平均每次获取时间应该小于5ms
    }).catch(() => false)
  })()
)

// 测试并发获取性能
console.log(
  "并发获取性能:", (() => {
    const obj = { a: 1, b: 2, c: 3, d: 4, e: 5 }
    const paths = ['a', 'b', 'c', 'd', 'e']
    const iterations = 100
    const promises = []

    const start = performance.now()
    for (let i = 0; i < iterations; i++) {
      promises.push(Get.getConcurrent(obj, paths, 'default', async (value, key) => {
        await new Promise(resolve => setTimeout(resolve, 1))
        return value * 2
      }, 3))
    }

    return Promise.all(promises).then(results => {
      const end = performance.now()
      const avgTime = (end - start) / iterations

      return results.length === iterations &&
        results.every(result => Array.isArray(result) && result.length === 5) &&
        avgTime < 10 // 平均每次获取时间应该小于10ms
    }).catch(() => false)
  })()
)

// 测试复杂结构获取性能
console.log(
  "复杂结构获取性能:", (() => {
    const obj = {
      users: Array.from({ length: 100 }, (_, i) => ({
        id: i,
        profile: { name: `User${i}`, age: 20 + i },
        posts: Array.from({ length: 10 }, (_, j) => ({
          id: j,
          title: `Post ${j}`,
          likes: j * 10
        }))
      })),
      meta: { total: 100, page: 1 }
    }

    const iterations = 1000

    const start = performance.now()
    for (let i = 0; i < iterations; i++) {
      Get.get(obj, `users.${i % 100}.profile.name`)
    }
    const end = performance.now()

    const avgTime = (end - start) / iterations
    return avgTime < 0.01 // 平均每次获取时间应该小于0.01ms
  })()
)

// 测试内存使用性能
console.log(
  "内存使用性能:", (() => {
    const obj = { a: { b: { c: 1 } } }
    const iterations = 10000
    const results = []

    for (let i = 0; i < iterations; i++) {
      const result = Get.get(obj, 'a.b.c')
      results.push(result)
    }

    return results.length === iterations &&
      results.every(result => result === 1)
  })()
)

console.log("=== Get 性能测试完成 ===")
